package com.anlogic.sdk.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtils {
	public static final String COMMA_DELIMITER_NO_SPACE = ",";

	public static final String COMMA_DELIMITER = ", ";

	public static final String NEW_LINE = System.getProperty("line.separator");

	public static boolean isEmpty(String string) {
		return !(string != null && !string.trim().isEmpty());
	}

	public static boolean isNotEmpty(String string) {
		return (string != null && !string.trim().isEmpty());
	}

	public static String getNotEmpty(String string) {
		if (string == null)
			return "";
		return string;
	}

	public static String getNonNullString(String string) {
		return (string == null) ? "" : string;
	}

	public static String getSerializableCSVString(Collection<?> objects) {
		return getCharSeparatedString(objects, COMMA_DELIMITER_NO_SPACE);
	}

	public static String getCSVString(Collection<?> objects) {
		return getCharSeparatedString(objects, COMMA_DELIMITER);
	}

	public static String getCharSeparatedString(Collection<?> objects, String delimiter) {
		if (objects == null || objects.isEmpty())
			return null;
		StringBuilder builder = new StringBuilder();
		for (Object object : objects) {
			builder.append(object.toString());
			builder.append(delimiter);
		}
		return builder.substring(0, builder.length() - delimiter.length());
	}

	public static List<String> convertSerializableToList(String csvString) {
		return convertToList(csvString, COMMA_DELIMITER_NO_SPACE);
	}

	public static List<String> convertToList(String csvString) {
		return convertToList(csvString, COMMA_DELIMITER);
	}
	
	public static Map<String, String> convertToMap(String csvString) {
		return convertToMap(csvString, COMMA_DELIMITER);
	}

	public static List<String> convertToList(String csvString, String delimiter) {
		if (isEmpty(csvString))
			return Collections.emptyList();
		return Arrays.asList(csvString.split(delimiter));
	}
	
	public static Map<String, String> convertToMap(String csvString, String delimiter) {
		if (isEmpty(csvString))
			return Collections.emptyMap();
		List<String> list = Arrays.asList(csvString.split(delimiter));
		Map<String, String> map = new HashMap<String, String>();
		for (String entry : list) {
			String[] entries = entry.split("=");
			String key = entries[0].trim();
			String value = "";
			if (entries.length >= 2)
				value = entries[1].trim();
			map.put(key, value);
		}
		return map;
	}

	public static String getDateFromString(String str) {
		Pattern p = Pattern.compile("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
		Matcher m = p.matcher(str);
		if (m.find()) {
			return m.group().toString();
		}
		return "";
	}

	public static String join(String[] strArray, String joiner) {
		StringBuffer sb = new StringBuffer();
		int i = 0;
		for (String item : strArray) {
			if (i > 0) {
				sb.append(joiner);
			}
			sb.append(item.trim());
			++i;
		}
		return sb.toString();
	}
	
	public static String duplicateBackslashes(String str) {

		if (str.indexOf('\\') < 0) {
			return str;
		}

		String sa[] = str.split("\\\\\\\\");
		for (int i = 0; i < sa.length; ++i) {
			sa[i] = sa[i].replaceAll("\\\\", "\\\\\\\\");
		}

		str = StringUtils.join(sa, "\\\\");
		return str;
	}
	
	public static long parseLong(String str) {
		if (str == null || str.isEmpty())
			return -1L;
		try {
			if (str.startsWith("0x"))
				return Long.parseLong(str.substring(2), 16);
			return Long.parseLong(str);
		} catch (Exception exception) {
			return -1L;
		}
	}
	
	public static int parseInt(String str) {
		if (str == null || str.isEmpty())
			return -1;
		try {
			if (str.startsWith("0x"))
				return Integer.parseInt(str.substring(2), 16);
			return Integer.parseInt(str);
		} catch (Exception exception) {
			return -1;
		}
	}
}
